package interview.threadpool;


import java.util.concurrent.TimeUnit;

class MyThread implements Runnable{
    private String lockA;
    private String lockB;
    public MyThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }
    @Override
    public void run() {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+"自己持有锁"+lockA+"尝试获得"+lockB);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+"自己持有锁"+lockB+"尝试获得"+lockA);
            }
        }

    }
}
public class DeadLock {
    public static void main(String[] args) {
        String lockA = "AAA";
        String lockB = "BBB";
        new Thread(new MyThread(lockA,lockB)).start();
        new Thread(new MyThread(lockB,lockA)).start();
    }
}
